package com.weishi.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static Date getSpecifiedDayAfter(Date date, int afterDay) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + afterDay);
        return c.getTime();
    }

    /**
     * 获取本月第一天
     *
     * @param date
     * @return
     */
    public static String getMonthFirstDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);//获取当月第一天
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd");
    }

    public static String getMonthFirstDay(Date date, int def_month) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.set(Calendar.DAY_OF_MONTH, 1);//获取当月第一天
        c.add(Calendar.MONTH, def_month);//下几个月
        return DateFormatUtils.format(c.getTime(), "yyyy-MM-dd");
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 计算当前时间是否在两个时间之内
     */
    public static boolean isDateBetweenOtherDate(Date startDate, Date endDate, Date nowDate) {
        // 获得两个时间的毫秒时间差异
        boolean start = startDate.getTime() <= nowDate.getTime();
        boolean end = endDate.getTime() >= nowDate.getTime();
        return start && end;
    }

    /**
     * 计算两个时间的差值（天）
     */
    public static int getTimeDifference(Date startDate, Date endDate) {
        // 获得两个时间的毫秒时间差异
        long day = endDate.getTime() / startDate.getTime();
        return (int)day;
    }


    /**
     * 获取一个临时的时间戳（距离2021-01-01 00:00:00的时间戳）
     * @return
     */
    public static long getTempTimp(long timp){
        Date date = DateUtils.parseDate("2021-01-01 00:00:00");
        return getTempTimp(timp,date);
    }

    public static long getTempTimp(long timp, Date date){
        return timp - date.getTime();
    }

    /**
     * 00:00:26.60 转化成毫秒
     * @param strTime
     * @return
     */
    public static long getLongTime(String strTime){
        if(StringUtils.isEmpty(strTime)){
            return 0;
        }
        String[] ss = strTime.split("\\.");
        String[] as =ss[0].split(":");
        int sss = (int) (Double.parseDouble("0."+ss[1])*1000);
        long longTime = (Integer.parseInt(as[0])*60*60 + Integer.parseInt(as[1])*60 + Integer.parseInt(as[2]))*1000 + sss;
        return longTime;
    }

    /**
     * 毫秒转化成 0:0:26
     * @param time
     * @return
     */
    public static String getStringTime(long time){
        if(time<=0){
            return "0:0:0";
        }
        long S = Math.round(time/1000.0);
        long ss = S%60;
        long mm = (S/60)%60;
        long hh = (S/60/60)%24;

      return hh+":"+mm+":"+ss;
    }

    public static void main(String[] args) {
        long ff = 1630575794017L;
        String a1 = getStringTime(1630575794982L-ff);
        System.out.println(a1);
        String aa = getStringTime(1630575796022L-ff);
        System.out.println(aa);
    }

    /**
     * 00:00:26.600 转化成毫秒
     * @param time
     * @return
     */
    public static String getStringTime_HHmmss_SS(long time){
        if(time<=0){
            return "00:00:00.0";
        }
        long S = time%1000;
        long ss = (time/1000)%60;
        long mm = (time/1000/60)%60;
        long hh = (time/1000/60/60)%24;

      return formatString_HHmmss(hh+":"+mm+":"+ss)+"."+S;
    }






    /**
     * ********************   计算时间的前几个小时  ， 和后几个小时   **************************************************************
     */

    /**
     * 返回 String 类型 前几个小时的方法
     * time 传入的时间 例如       2021-05-28 10:06:12
     * ihour 相差的小时 例如     5
     * 前几小时写负数   -5
     * 后几小时写正数    5
     */
    public static String lastOrAfterHour(String times , int ihour) {
        String forecastTime = new String();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(times);
            calendar.setTime(date);
            calendar.add(Calendar.HOUR_OF_DAY, ihour);
            forecastTime = simpleDateFormat.format(calendar.getTime());
            return forecastTime;
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return "";
    }

    /**
     * ********   计算时间的前几个小时  ， 和后几个小时，结束   **************************************************************
     */

    /**
     * 转换时间格式  0:0:0 => 00:00:00
     * @param time
     * @return
     */
    public static String formatString_HHmmss(String time) {
        String aa[] = time.split(":");
        String ret = "";
        for(String s : aa){
            if(s.length()==1){
                s = "0" + s;
            }
            ret += s + " ";
        }
        ret = ret.trim();
        return ret.replace(" ",":");
    }

    /**
     * 转换时间格式  00:00:00 => 0:0:0
     * @param time
     * @return
     */
    public static String formatString_Hms(String time) {
        long tt = getLongTime(time);
        return getStringTime(tt);
    }











}
